import 'dart:io';

import 'package:flustars_flutter3/flustars_flutter3.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_taotaoshipin/common/theme/app_colors.dart';
import 'package:flutter_taotaoshipin/util/device_utils.dart';
import 'package:flutter_taotaoshipin/util/global.dart';
import 'package:flutter_taotaoshipin/value/sp_key.dart';
import 'package:flutter_taotaoshipin/widget/load_image.dart';
import 'package:get/get.dart';

class PrivacyDialog extends StatelessWidget {
  const PrivacyDialog({super.key});

  @override
  Widget build(BuildContext context) {
    /// Android 11添加了键盘弹出动画，这与我添加的过渡动画冲突（原先iOS、Android 没有相关过渡动画，相关问题跟踪：https://github.com/flutter/flutter/issues/19279）。
    /// 因为在Android 11上，viewInsets的值在键盘弹出过程中是变化的（以前只有开始结束的值）。
    /// 所以解决方法就是在Android 11及以上系统中使用Padding代替AnimatedPadding。
    if (Device.getAndroidSdkInt() >= 30) {
      return Padding(
        padding: context.mediaQueryViewInsets,
        child: _buildContent(context),
      );
    } else {
      return AnimatedPadding(
        padding: context.mediaQueryViewInsets,
        duration: const Duration(milliseconds: 120),
        curve: Curves.easeInCubic,
        child: _buildContent(context),
      );
    }
  }

  _buildContent(BuildContext context) {
    return MediaQuery.removeViewInsets(
      context: context,
      removeLeft: true,
      removeTop: true,
      removeRight: true,
      removeBottom: true,
      child: Center(
        child: SizedBox(
          width: 307,
          child: _buildBody(context),
        ),
      ),
    );
  }

  _buildBody(BuildContext context) {
    return Material(
      borderRadius: BorderRadius.circular(15),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Container(
            width: double.infinity,
            height: 88,
            decoration: BoxDecoration(
              gradient: LinearGradient(
                colors: [
                  Color(0xFFB9D6FF),
                  Colors.white,
                ],
                begin: Alignment.topCenter,
                end: Alignment.bottomCenter,
              ),
              borderRadius: BorderRadius.vertical(top: Radius.circular(15)),
            ),
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Expanded(
                  child: Padding(
                    padding: const EdgeInsets.only(top: 39.5, left: 26.5),
                    child: Text(
                      '温馨提示',
                      style: TextStyle(
                        color: AppColors.text,
                        fontSize: 18,
                        fontWeight: FontWeight.w900,
                      ),
                    ),
                  ),
                ),
                Padding(
                  padding: EdgeInsets.only(top: 15, right: 22),
                  child: LoadAssetImage(
                    'splash/ring',
                    width: 69,
                    height: 57,
                  ),
                ),
              ],
            ),
          ),
          _buildPrivacyText(),
          Padding(
            padding: const EdgeInsets.symmetric(horizontal: 28),
            child: GestureDetector(
              onTap: () {
                SpUtil.putBool(SpKey.privacy, true);
                Get.back();
              },
              child: Container(
                height: 50,
                alignment: Alignment.center,
                decoration: BoxDecoration(
                  gradient: LinearGradient(
                    colors: [
                      Color(0xFF7AB3FF),
                      Color(0xFF2760FF),
                    ],
                    begin: Alignment.topCenter,
                    end: Alignment.bottomCenter,
                  ),
                  borderRadius: BorderRadius.circular(10),
                ),
                child: Text(
                  '同意',
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: 16,
                    fontWeight: FontWeight.w900,
                  ),
                ),
              ),
            ),
          ),
          Padding(
            padding: const EdgeInsets.all(20),
            child: GestureDetector(
              onTap: () {
                exit(0);
              },
              child: Container(
                child: Text(
                  '不同意，退出APP',
                  style: TextStyle(
                    color: Color(0xFFAEBCDB),
                    fontSize: 16,
                  ),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildPrivacyText() {
    return Padding(
      padding: const EdgeInsets.fromLTRB(28, 5, 28, 19),
      child: RichText(
        text: TextSpan(
          text: '请您在使用本产品之前仔细阅读',
          style: TextStyle(color: AppColors.text, height: 1.5),
          children: [
            TextSpan(
                text: "《用户协议》",
                style: const TextStyle(color: AppColors.app_main),
                recognizer: TapGestureRecognizer()
                  ..onTap = () {
                    //打开用户协议
                    Global.openUserProtocol();
                  }),
            TextSpan(text: "、"),
            TextSpan(
                text: "《隐私协议》",
                style: const TextStyle(color: AppColors.app_main),
                recognizer: TapGestureRecognizer()
                  ..onTap = () {
                    //打开隐私协议
                    Global.openPravicyPolicy();
                  }),
            TextSpan(text: "和苹果公司标准"),
            TextSpan(
                text: "《最终用户许可协议》",
                style: const TextStyle(color: AppColors.app_main),
                recognizer: TapGestureRecognizer()
                  ..onTap = () {
                    //打开隐私协议
                    Global.openAppleEULA();
                  }),
            TextSpan(
              text:
                  '。\n\n如您点击“同意”，即表示您已仔细阅读并同意，我们将尽全力保障您的合法权益并继续为您提供优质的产品和服务。如您点击“不同意”，将可能导致您无法继续使用我们的产品和服务。',
            ),
          ],
        ),
      ),
    );
  }
}
